<template>
	<view>
		<unicloud-db ref="udb" v-slot:default="{data}" :collection="collectionList"
			 @load="load">
			<homeworkCard :data="data" :userInfo="userInfo" :single="single"></homeworkCard>
			<view class="submitInfo">
			</view>
		</unicloud-db>
	</view>
	<!-- 用户提交的数据 -->
	<view class="user-submit">
		<!-- card -->
		<uni-section class="mb-10" title="作答" type="line"></uni-section>
		<uni-card :is-shadow="false" :modelValue="formData">
			<uni-forms ref="form">
				<uni-forms-item >
					<uni-easyinput type="textarea" v-model="formData.value" placeholder="请输入内容" @input="input"></uni-easyinput>
				</uni-forms-item>
				<uni-forms-item name="name">
					<view class="example-body">
						<uni-file-picker limit="9" title="最多选择9张图片" v-model="formData.images" ></uni-file-picker>
					</view>
				</uni-forms-item>
			</uni-forms>
		</uni-card>
		<!-- 提交按钮 -->
		<uni-button class="submit-btn" type="primary" @click="submit">提交</uni-button>
	</view>

</template>

<script>
import HomeworkCard from '../../components/homework-card/homework-card.vue';
const db = uniCloud.database()
	export default {
		components: {
			HomeworkCard
		},
		data() {
			return {
				user_id:"",
				// 数据表名
				collectionList: [db.collection('assig-homework').field(
						'class_id,homework_title,homework_detail,homework_image,homework_video,create_homework_time,is_show').getTemp(), db
					.collection('class-list').field('_id,class_name as text,invitation_code,user_id').getTemp()
				],
				formData: {
					status: 'loading' // 加载状态
				},
				tipShow: false, // 是否显示顶部提示框
				// 作业标题
				homework: '最新作业',
				// 当前日期
				single: '',
				formData: {
					//用户输入的文字
					value: '',
					//用户选择的图片
					images: [],
				},
				// 当前作业id
				homework_id: '',
			};
		},
		onLoad(options) {
			// 获取缓存里的数据
			let homework_id = uni.getStorageSync('data')._id
			// 设置当前作业id
			this.homework_id = homework_id
			console.log('homework_id',homework_id);
		},
		methods:{
			input(e) {
				// console.log('输入内容：', e);
			},
			// 提交作业
			submit(){
				//获取用户id
				let user_id = this.userInfo._id
				// console.log('user_id',user_id,this.userInfo)
				// 获取用户名字
				let user_name = this.userInfo.username
				// 查询用户所在班级
				db.collection('class-list').where({
					user_id: user_id
				}).get().then(res => {
					// 获取班级id,提交的时候用
					let class_id = res.result.data[0]._id
					
					//查看submit-homework里面存不存在这个用户的当前作业

					db.collection('submit-homework').where({
						user_id: user_id,
						class_id: class_id,
						homework_id: this.homework_id
					}).get().then(res => {
						console.log('提交作业数据库res',res)
						// 如果存在
						if(res.result.data.length > 0){
							// 更新作业
							db.collection('submit-homework').where({
								user_id: user_id
							}).update({
								user_id: user_id,
								user_name: user_name,
								class_id: class_id,
								homework_value: this.formData.value,
								homework_image: this.formData.images,
								homework_create_time: new Date().toLocaleString(),
								is_finish: true,
								homework_id: this.homework_id
							}).then(res => {
								console.log('更新成功',res)
								uni.showToast({
									title: '更新成功'
								})
								setTimeout(() => {
									// 返回上一页
									uni.navigateBack()
									// 刷新当前页面
									
								}, 500);
							}).catch(err => {
								console.log('更新失败',err)
							})
						}else{
							// 添加作业
							db.collection('submit-homework').add({
								user_id: user_id,
								user_name: user_name,
								class_id: class_id,
								homework_value: this.formData.value,
								homework_image: this.formData.images,
								homework_create_time: new Date().toLocaleString(),
								is_finish: true,
								homework_id: this.homework_id
							}).then(res => {
								console.log('提交成功',res)
								uni.showToast({
									title: '提交成功'
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 500);
							}).catch(err => {
								console.log('提交失败',err)
							})
						}
					}).catch(err => {
						console.log('查询失败',err)
					})
				})

				console.log('this.formData',this.formData)
			},
			// 提交表单
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
				uni.showToast({
					title: '新增成功'
				})
				this.getOpenerEventChannel().emit('refreshData')
				setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
				uni.showModal({
					content: err.message || '请求服务失败',
					showCancel: false
				})
				})
			},
			
		},
		computed: {
			userInfo() {
				return uni.getStorageSync('data').userInfo
			},
			single(){
				return uni.getStorageSync('data').single
			}
		},
	}
</script>

<style lang="scss">
	.mb-10 {
		margin-bottom: 10px;
	}

</style>
